package com.mycompany.lctwitter.aop;

import java.io.IOException;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.mycompany.lctwitter.action.AbstractAction;
import com.mycompany.lctwitter.biz.model.UserModel;
import com.opensymphony.xwork2.ActionContext;

@Aspect
@Component
public class SecurityAspect {

	@Before("execution(* com.mycompany.lctwitter.action..*.execute(..)) && !execution(* com.mycompany.lctwitter.action.user.Login*.*(..))")
	public void checkLoginStatus() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserModel user = ((UserModel) session.get(AbstractAction.LOGINED_USER));
		if (user == null) {
			try {
				ServletActionContext.getResponse().sendRedirect("/lctwitter/user/login-page");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
